package cn.lg.soar.system.monitor.client;

import cn.lg.soar.common.model.LogDTO;
import cn.lg.soar.common.util.BeanCopier;
import cn.lg.soar.core.annotations.ApiClient;
import cn.lg.soar.core.handler.log.IOperationLogSaveService;
import cn.lg.soar.system.api.constant.ModuleConst;
import cn.lg.soar.system.monitor.entity.OperationLog;
import cn.lg.soar.system.monitor.service.OperationLogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import java.util.Collection;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 简单日志接口
 * @author luguoxiang
 * @date 2021-08-11
 * 开源项目：https://gitee.com/lgx1992/lg-soar 求star！请给我star！请帮我点个star！
 */
@ApiIgnore
@Primary
@ApiClient("boot")
@RestController
@RequestMapping(ModuleConst.CLIENT_PATH + "/log")
public class OperationLogApiClient implements IOperationLogSaveService {

    @Autowired
    private OperationLogService service;

    private final static BeanCopier<LogDTO, OperationLog> TO_ENTITY = BeanCopier.create(LogDTO.class, OperationLog.class);

    /**
     * 保存操作日志到数据库
     * @param logs
     */
    @PostMapping("saveLog")
    @Override
    public void saveLog(@RequestBody Collection<? extends LogDTO> logs) {
        List<OperationLog> list = TO_ENTITY.convertBatch(logs).collect(Collectors.toList());
        service.saveBatch(list);
    }

}
